/*
 * Copyright 2022 emo Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.wongxd.demo.page.emo.ui.page

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.unit.dp
import io.modifier.basic.emo.modal.ClickPositionCheckerBox
import io.modifier.basic.emo.modal.EmoBottomSheetList
import io.modifier.basic.emo.modal.EmoDialogActions
import io.modifier.basic.emo.modal.EmoDialogList
import io.modifier.basic.emo.modal.EmoDialogMarkList
import io.modifier.basic.emo.modal.EmoDialogMsg
import io.modifier.basic.emo.modal.EmoDialogMutiCheckList
import io.modifier.basic.emo.modal.EmoModalAction
import io.modifier.basic.emo.modal.QuickAction
import io.modifier.basic.emo.modal.TipStatus
import io.modifier.basic.emo.modal.emoBottomSheet
import io.modifier.basic.emo.modal.emoDialog
import io.modifier.basic.emo.modal.emoPopup
import io.modifier.basic.emo.modal.emoQuickAction
import io.modifier.basic.emo.modal.emoTip
import io.modifier.basic.emo.modal.emoToast
import io.modifier.basic.emo.scheme.ComposeScheme
import io.modifier.basic.emo.ui.core.Item
import io.wongxd.demo.R
import io.wongxd.demo.page.emo.EmoMainActivity
import io.wongxd.demo.page.emo.config.SchemeConst
import io.wongxd.demo.page.emo.ui.CommonItem
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

@ComposeScheme(
    action = SchemeConst.SCHEME_ACTION_MODAL,
    alternativeHosts = [EmoMainActivity::class]
)
@Composable
fun ModalPage() {
    val view = LocalView.current
    OnlyBackListPage(
        title = "Modal"
    ) {
        item {
            CommonItem("消息类型对话框") {
                view.emoDialog { modal ->
                   MaterialTheme {
                        EmoDialogMsg(
                            modal,
                            "这是标题",
                            "这是一丢丢有趣但是没啥用的内容",
                            listOf(
                                EmoModalAction("取 消", MaterialTheme.colorScheme.primary) {
                                    it.dismiss()
                                },
                                EmoModalAction("确 定", MaterialTheme.colorScheme.primary) {
                                    view.emoToast("确定啦!!!")
                                    it.dismiss()
                                }
                            )
                        )
                    }
                }.show()
            }
        }

        item {
            CommonItem("列表类型对话框") {
                view.emoDialog { modal ->
                    EmoDialogList(modal, maxHeight = 500.dp) {
                        items(200) { index ->
                            Item(title = "第${index + 1}项") {
                                view.emoToast("你点了第${index + 1}项")
                            }
                        }
                    }
                }.show()
            }
        }

        item {
            CommonItem("单选类型浮层") {
                view.emoDialog { modal ->
                    val list = remember {
                        val items = arrayListOf<String>()
                        for (i in 0 until 500) {
                            items.add("Item $i")
                        }
                        items
                    }
                    var markIndex by remember {
                        mutableStateOf(20)
                    }
                    EmoDialogMarkList(
                        modal,
                        maxHeight = 500.dp,
                        list = list,
                        markIndex = markIndex
                    ) { _, index ->
                        markIndex = index
                        view.emoToast("你点了第${index + 1}项")
                    }
                }.show()
            }
        }

        item {
            CommonItem("多选类型浮层") {
                view.emoDialog { modal ->
                    MaterialTheme {
                        val list = remember {
                            val items = arrayListOf<String>()
                            for (i in 0 until 500) {
                                items.add("Item $i")
                            }
                            items
                        }
                        val checked = remember {
                            mutableStateListOf(0, 5, 10, 20)
                        }
                        val disable = remember {
                            mutableStateListOf(5, 10)
                        }
                        Column {
                            EmoDialogMutiCheckList(
                                modal,
                                maxHeight = 500.dp,
                                list = list,
                                checked = checked.toSet(),
                                disabled = disable.toSet()
                            ) { _, index ->
                                if (checked.contains(index)) {
                                    checked.remove(index)
                                } else {
                                    checked.add(index)
                                }
                            }
                            EmoDialogActions(
                                modal = modal,
                                actions = listOf(
                                    EmoModalAction("取 消", MaterialTheme.colorScheme.primary) {
                                        it.dismiss()
                                    },
                                    EmoModalAction("确 定", MaterialTheme.colorScheme.primary) {
                                        view.emoToast("你选择了: ${checked.joinToString(",")}")
                                        it.dismiss()
                                    }
                                )
                            )
                        }
                    }
                }.show()
            }
        }

        item {
            CommonItem("Toast") {
                view.emoToast("这只是个 Toast!")
            }
        }

        item {
            CommonItem("BottomSheet(list)") {
                view.emoBottomSheet {
                    EmoBottomSheetList(it) {
                        items(200) { index ->
                            Item(title = "第${index + 1}项") {
                                view.emoToast("你点了第${index + 1}项")
                            }
                        }
                    }
                }.show()
            }
        }

        item {
            val scope = rememberCoroutineScope()
            CommonItem("Tip - Done") {
                val flow = MutableStateFlow<TipStatus>(TipStatus.Loading())
                val tip = view.emoTip(status = flow).show()
                scope.launch {
//                    delay(1000)
//                    flow.value = TipStatus.Done()
//                    delay(100)
                    tip.dismiss()
                }
            }
        }

        item {
            val scope = rememberCoroutineScope()
            CommonItem("Tip - Error") {
                val flow = MutableStateFlow<TipStatus>(TipStatus.Loading())
                val tip = view.emoTip(status = flow).show()
                scope.launch {
                    delay(1000)
                    flow.value = TipStatus.Error()
                    delay(100)
                    tip.dismiss()
                }
            }
        }

        item {
            ClickPositionCheckerBox(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp, vertical = 8.dp)
                    .clip(RoundedCornerShape(6.dp))
                    .background(MaterialTheme.colorScheme.surfaceVariant),
                onClick = {
                    view.emoPopup(
                        offset = it,
                        widthCal = {
                            200.dp
                        },
                        background = { MaterialTheme.colorScheme.secondaryContainer }
                    ){
                        Text(
                            text = "This is Popup Content\nThis is Popup Content",
                            modifier = Modifier.padding(16.dp),
                            color = MaterialTheme.colorScheme.onSecondaryContainer
                        )
                    }.show()
                }
            ) {
                Text(
                    text = "PopUp",
                    modifier = Modifier.padding(horizontal = 16.dp, vertical = 14.dp)
                )
            }
        }

        item {
            ClickPositionCheckerBox(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp, vertical = 8.dp)
                    .clip(RoundedCornerShape(6.dp))
                    .background(MaterialTheme.colorScheme.surfaceVariant),
                onClick = {
                    view.emoQuickAction(
                        offset = it,
                        actions = listOf(
                            QuickAction(R.drawable.quick_action_ic_copy, "Copy") {
                                view.emoToast("What do you want to copy?").show()
                            },
                            QuickAction(R.drawable.quick_action_ic_delete, "Delete") {
                                view.emoToast("What do you want to delete?").show()
                            }
                        ),
                        actionWidth = 48.dp
                    ).show()
                }
            ) {
                Text(
                    text = "Quick Action",
                    modifier = Modifier.padding(horizontal = 16.dp, vertical = 14.dp))
            }
        }

        item {
            Box(modifier = Modifier.height(80.dp))
        }

        item {
            ClickPositionCheckerBox(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp, vertical = 8.dp)
                    .clip(RoundedCornerShape(6.dp))
                    .background(MaterialTheme.colorScheme.surfaceVariant),
                onClick = {
                    view.emoPopup(
                        offset = it,
                        widthCal = {
                            200.dp
                        },
                        background = { MaterialTheme.colorScheme.secondaryContainer }
                    ){
                        Text(
                            text = "This is Popup Content\nThis is Popup Content",
                            modifier = Modifier.padding(16.dp),
                            color = MaterialTheme.colorScheme.onSecondaryContainer
                        )
                    }.show()
                }
            ) {
                Text(
                    text = "PopUp",
                    modifier = Modifier.padding(horizontal = 16.dp, vertical = 14.dp)
                )
            }
        }

        item {
            Box(modifier = Modifier.height(80.dp))
        }

        item {
            ClickPositionCheckerBox(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp, vertical = 8.dp)
                    .clip(RoundedCornerShape(6.dp))
                    .background(MaterialTheme.colorScheme.surfaceVariant),
                onClick = {
                    view.emoQuickAction(
                        offset = it,
                        actions = listOf(
                            QuickAction(R.drawable.quick_action_ic_copy, "Copy") {
                                view.emoToast("What do you want to copy?").show()
                            },
                            QuickAction(R.drawable.quick_action_ic_delete, "Delete") {
                                view.emoToast("What do you want to delete?").show()
                            }
                        ),
                        actionWidth = 48.dp
                    ).show()
                }
            ) {
                Text(
                    text = "Quick Action",
                    modifier = Modifier.padding(horizontal = 16.dp, vertical = 14.dp))
            }
        }

        item {
            Box(modifier = Modifier.height(80.dp))
        }


    }
}
